import React, { Component } from 'react'
import { Button, PageHeader, Breadcrumb } from 'antd'
import { withRouter } from 'react-router-dom'
import { BackwardOutlined } from '@ant-design/icons';
import navDate from '@/assets/utils/leftNav'
class CommPageHeader extends Component {
    constructor(props) {
        super(props)
        let pathname = this.props.history.location.pathname
        this.state = {
            breadcrumb: this.getName(pathname)
        }

    }
    getName(pathname) {
        let nameArr = [];

        for (let i in navDate) {

            if (navDate[i].path == pathname) {
                nameArr.push(navDate[i].title)
                return nameArr
            } else if (navDate[i].children && navDate[i].children.length) {
                for (let k in navDate[i].children) {
                    if (navDate[i].children[k].path == pathname) {
                        nameArr.push(navDate[i].title)
                        nameArr.push(navDate[i].children[k].title)
                        return nameArr
                    }
                }

            }
        }
        return nameArr
    }
    componentWillReceiveProps(nextProps) {
        let pathname = nextProps.location.pathname;
        if (pathname !== this.props.location.pathname) {
            this.setState({ breadcrumb: this.getName(pathname) })
        }
    }
    backFn = () => {
        this.props.history.goBack()
    }
    render() {
        return (
            <PageHeader className="page-header " title="" subTitle="">
                <div className="flex align-items">
                    <Breadcrumb className="flex-1">
                        {this.state.breadcrumb.map(item => {
                            return <Breadcrumb.Item key={item}>{item}</Breadcrumb.Item>
                        })}
                    </Breadcrumb>
                    <Button icon={<BackwardOutlined />} type="primary" onClick={this.backFn} danger>返回</Button>
                </div>
            </PageHeader>
        )
    }
}
export default withRouter(CommPageHeader)